package cnblogs2;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectMySQL {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException {
		//201521123065
		String URL = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=GBK";
		String driverName = "com.mysql.jdbc.Driver";
		String sql = "select * from stuinfor";
		String userName = "root";//root
		String password = "conan92936";//123456
		Connection conn = null;
		
		Class.forName(driverName);//jdbc4.0 锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷注锟斤拷锟斤拷锟�
		
		
		try {
			conn = DriverManager.getConnection(URL,userName,password);
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			// id | stuno     | name | gender | birthdate  | major
			while(resultSet.next()){
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Date date = resultSet.getDate("birthday");
				System.out.print("id="+id+" name="+name+" birthday="+date);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//锟斤拷锟斤拷使锟斤拷Try..with..resources锟斤法锟斤拷
			conn = null;
		}
	}
	
}
